//
//  RentalDemandViewController.m
//  Fangmm
//
//  Created by 阮琦 on 16/3/8.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "RentalDemandViewController.h"
#import "EditRequirementViewController.h"

#import "RentalDemandTableViewCell.h"

#import "ACMacros.h"
#import "Utility.h"
#import "Constants.h"

#import "HttpServer.h"
#import "Requirement.h"

#import <TSMessage.h>
#import <MobClick.h>

@interface RentalDemandViewController () <UITableViewDataSource, UITableViewDelegate>

@end

@implementation RentalDemandViewController {
    Requirement *requirement;
    NSNumber *regionRate;
    NSNumber *rentRate;
    NSNumber *dayRate;
    NSNumber *regionTotal;
    NSNumber *rentTotal;
    NSNumber *regionIncrease;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    regionRate = 0;
    rentRate = 0;
    dayRate = 0;
    regionTotal = 0;
    rentTotal = 0;
    regionIncrease = 0;
    [self configureNavigationBar];
    [self configureTableView];
    // Do any additional setup after loading the view.
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (self.tableView) {
        [self loadRequirementData];
    }
    [MobClick beginLogPageView:@"租房需求页"];//("PageOne"为页面名称，可自定义)
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"租房需求页"];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - Configuring the view’s layout behavior
- (void)configureNavigationBar {
    self.title = @"租房需求";
    self.navigationItem.hidesBackButton = true;
    UIImage *backImage = [[UIImage imageNamed:@"back-18-36"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]initWithImage:backImage style:UIBarButtonItemStyleDone target:self action:@selector(clickToPopBack)];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    UIImage *editImage = [[UIImage imageNamed:@"need_red-40-36"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc]initWithImage:editImage style:UIBarButtonItemStyleDone target:self action:@selector(clickToEdit)];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    self.navigationItem.rightBarButtonItem = rightBarButton;
}

- (void)configureTableView {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Height)];
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    [self.tableView registerNib:[UINib nibWithNibName:@"RentalDemandTableViewCell" bundle:nil] forCellReuseIdentifier:@"RentalDemandTableViewCell"];
    [self.view addSubview:self.tableView];
}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (requirement == nil) {
        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        return cell;
    } else {
        RentalDemandTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RentalDemandTableViewCell"];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        NSInteger section = indexPath.section;
        switch (section) {
            case 0:
                [cell.titleLabel setText:@"目标区域"];
                [cell.contentLabel setText:[NSString stringWithFormat:@"%@-%@", requirement.district , requirement.region]];
                [cell.chartLabel setText:[NSString stringWithFormat:@"%@%%",regionRate]];
                if ([requirement.region isEqual:@"全部"]) {
                    [cell.chartTag setText:[NSString stringWithFormat:@"%@%%的用户想住在%@",regionRate,requirement.district]];
                    [cell.footerLabel setText:[NSString stringWithFormat:@"%@附近已有%@套房源,平均每天增加%@套",requirement.district,regionTotal,regionIncrease]];
                } else {
                    [cell.chartTag setText:[NSString stringWithFormat:@"%@%%的用户想住在%@",regionRate,requirement.region]];
                    [cell.footerLabel setText:[NSString stringWithFormat:@"%@附近已有%@套房源,平均每天增加%@套",requirement.region,regionTotal,regionIncrease]];
                }
                
                break;
            case 1:
                [cell.titleLabel setText:@"租金预算"];
                [cell.contentLabel setText:[NSString stringWithFormat:@"%@", requirement.price]];
                [cell.chartLabel setText:[NSString stringWithFormat:@"%@%%",rentRate]];
                [cell.chartTag setText:[NSString stringWithFormat:@"%@%%的用户和的预算大致相同",rentRate]];
                [cell.footerLabel setText:[NSString stringWithFormat:@"目标区域在预算范围可搭伙的房源有%@套",rentTotal]];
                break;
            case 2:
                [cell.titleLabel setText:@"预住时间"];
                [cell.preContentLabel setText:@"即日起"];
                [cell.contentLabel setText:[NSString stringWithFormat:@"%d天内",requirement.day]];
                [cell.chartLabel setText:[NSString stringWithFormat:@"%@%%",dayRate]];
                [cell.chartTag setText:[NSString stringWithFormat:@"%@%%的用户想在%d天内入住",dayRate,requirement.day]];
                break;
                
            default:
                break;
        }
        return cell;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 195;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

#pragma mark - Triggering Methods
- (void)clickToPopBack {
    [self.navigationController popToRootViewControllerAnimated:true];
}

- (void)clickToEdit {
    EditRequirementViewController *editRequirementVC = [[EditRequirementViewController alloc]init];
    [self.navigationController pushViewController:editRequirementVC animated:true];
}

- (void)loadRequirementData {
    [HttpServer getUserRequirementsWithSuccessBlock:^(NSDictionary *successDic) {
        Requirement *newRequirement = [[Requirement alloc]initWithDic:[successDic objectForKey:@"requirement"]];
        requirement = newRequirement;
        [Utility setObject:requirement key:kRequirement];
        [HttpServer getRequirementsStatistics:requirement WithSuccessBlock:^(NSDictionary *successDic) {
            regionRate = [[successDic objectForKey:@"region"]objectForKey:@"userRate"];
            regionTotal = [[successDic objectForKey:@"region"]objectForKey:@"houseTotal"];
            regionIncrease = [[successDic objectForKey:@"region"]objectForKey:@"houseRate"];
            rentRate = [[successDic objectForKey:@"rent"]objectForKey:@"userRate"];
            rentTotal = [[successDic objectForKey:@"region"]objectForKey:@"houseTotal"];
            dayRate = [[successDic objectForKey:@"day"]objectForKey:@"userRate"];
            [self.tableView reloadData];
        } withFailBlock:^(id failData) {
            
        }];
    } withFailBlock:^(id failData) {
        
    }];
}

@end
